import { DatabaseModule } from '@libs/common/database';
import {
  PostStatusInterceptor,
  TransformInterceptor,
} from '@libs/common/interceptors';
import { LoggerModule } from '@libs/common/logger';
import { LoggerMiddleware } from '@libs/common/middleware';
import { RedisModule } from '@libs/common/redis';
import { getConfig } from '@libs/common/utils';
import {
  ClassSerializerInterceptor,
  MiddlewareConsumer,
  Module,
  NestModule,
} from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { ScheduleModule } from '@nestjs/schedule';

import { DeliverModule } from './atm/deliver/deliver.module';
import { DeviceCargoLaneModule } from './atm/device-cargo-lane/device-cargo-lane.module';
import { DeviceExchangeLogModule } from './atm/device-exchange-log/device-exchange-log.module';
import { DeviceRoadModule } from './atm/device-road/device-road.module';
import { DepartmentModule } from './dingding/department/department.module';
import { DepartmentConfigModule } from './dingding/department-config/department-config.module';
import { DingdingModule } from './dingding/dingding.module';
import { MemberModule } from './dingding/member/member.module';
import { AccessLogModule } from './hr/access-log/access-log.module';
import { AttendanceModule } from './hr/attendance/attendance.module';
import { AttendanceStatisticsModule } from './hr/attendance-statistics/attendance-statistics.module';
import { OvertimeModule } from './hr/overtime/overtime.module';
import { OvertimeApplyModule } from './hr/overtime-apply/overtime-apply.module';
import { OvertimeLogModule } from './hr/overtime-log/overtime-log.module';
import { OrderModule } from './market/order/order.module';
import { PointsModule } from './market/points/points.module';
import { PointsHistoryModule } from './market/points-history/points-history.module';
import { PointsWaitModule } from './market/points-wait/points-wait.module';
import { ProductModule } from './market/product/product.module';
import { AuthModule } from './user/auth/auth.module';
import { JwtAuthGuard } from './user/auth/guards/jwt.guard';
import { SysUserModule } from './user/sys-user/sys-user.module';

@Module({
  imports: [
    LoggerModule,

    DatabaseModule,
    ConfigModule.forRoot({
      ignoreEnvFile: true,
      isGlobal: true,
      load: [getConfig],
    }),
    ScheduleModule.forRoot(),
    EventEmitterModule.forRoot(),

    RedisModule,

    DingdingModule,

    MemberModule,

    AuthModule,

    SysUserModule,

    OrderModule,

    PointsModule,

    PointsHistoryModule,

    PointsWaitModule,

    ProductModule,

    DeliverModule,

    DeviceCargoLaneModule,

    DeviceRoadModule,

    DeviceExchangeLogModule,

    AttendanceModule,

    AttendanceStatisticsModule,

    AccessLogModule,

    DepartmentModule,

    DepartmentConfigModule,

    OvertimeModule,
    OvertimeLogModule,
    OvertimeApplyModule,
  ],
  providers: [
    {
      provide: APP_GUARD,
      useClass: JwtAuthGuard,
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: ClassSerializerInterceptor,
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: PostStatusInterceptor,
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: TransformInterceptor,
    },
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(LoggerMiddleware).forRoutes('*');
  }
}
